<template>
  <view class="goods-cart">
    
    <my-address v-if = 'cart.length !== 0'></my-address>
    <view class="back-img" v-if='cart.length === 0' >
      <image mode="widthFix" :src = 'emptyImg'></image>
      <text>空空如也</text>
    </view>
    <!-- 购物车标题 -->
   <view class="cart-title" v-if = 'cart.length !== 0'>
     <uni-icons type="shop" size="20"></uni-icons>
     <text class="cart-title-text">购物车</text>
   </view>
    
   <!-- 购物车列表 -->
    <!-- uni-swipe-action 是最外层包裹性质的容器 -->
    <uni-swipe-action v-if = 'cart.length !== 0'>
      <block v-for="(goods, i) in cart" :key="i">
        <!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过 options 属性来指定操作按钮的配置信息 -->
        <uni-swipe-action-item   :right-options="options" :options="options" @click="swipeActionClickHandler(goods)">
          <my-goods :goods="goods" :show-radio="true" :show-num="true" @radio-change="radioChangeHandler" @num-change="numberChangeHandler"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>


     <!-- 结算模块 -->
     <my-settle v-if = 'cart.length !== 0'></my-settle>
  </view>
</template>

<script>
 import badgeMix from '@/mixins/tabbar-badge.js'
 import { mapGetters } from 'vuex'
 // 按需导入 mapState 这个辅助函数
 import { mapState,mapMutations} from 'vuex'
  export default {
    mixins: [badgeMix],
  computed: {
    // 将 m_cart 模块中的 cart 数组映射到当前页面中使用
    ...mapState('m_cart', ['cart']),
    ...mapGetters('m_cart', ['total']),
    
    
  },
    data() {
      return {
        emptyImg:"/static/empty.png",
        options:[{
      text: '删除', // 显示的文本内容
      style: {
        backgroundColor: '#C00000' // 按钮的背景颜色
      }
    }]
      };
    },
    methods:{
      ...mapMutations('m_cart',['updateGoodsState','updateGoodsCount','removeGoodsById']),
      radioChangeHandler(e){
        // console.log(e)
        this.updateGoodsState(e)
      },
      numberChangeHandler(e) {
        this.updateGoodsCount(e)
      },
      swipeActionClickHandler(goods){
        this.removeGoodsById(goods.goods_id)
        //更新左上角图标
       this.setBadge()
      },
      setBadge() {
        // 调用 uni.setTabBarBadge() 方法，为购物车设置右上角的徽标
        uni.setTabBarBadge({
          index: 2,
          text: this.total + '', // 注意：text 的值必须是字符串，不能是数字
        })
      },
    }


  }
</script>

<style lang="scss">
.cart-title{
  display: flex;
  align-items: center;
  padding : 0 5px;
  border-bottom: 2px solid #efefef;
  uni-icons{
    flex: 1;
    display: flex;
    justify-content:center;
  }
  .cart-title-text{
    flex: 4;
    font-size: 15px;
    display:flex;
    align-items: center;
  }

}

.goods-cart{
  padding-bottom:50px;
  // display: flex;
  // flex-direction: column;
  // justify-content: center;
  .back-img{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 750rpx;
    text{
      position: relative;
      bottom:40px;
      color: #F09292;
      font-size: 25px;
      font-weight: 600;
    }
  }
}

</style>
